package com.office.automate.datagrid.framework;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.office.automate.configuration.property.DatagridPageConfiguration;
import com.office.automate.exception.framework.FrontierVisibleException;
import com.office.automate.exception.general.GeneralException;
import com.office.automate.fieldname.property.FieldNamesProperties;
import com.office.automate.model.employee.EmployeeSearch;
import com.office.automate.service.def.administration.AdministrationService;
import com.office.automate.service.def.employee.EmployeeService;
import com.office.automate.service.def.leave.EmployeeLeaveService;
import com.office.automate.service.impl.administration.AdministrationServiceImplementer;
import com.office.automate.service.impl.employee.EmployeeServiceImplementer;
import com.office.automate.service.impl.leave.EmployeeLeaveServiceImplementer;

public class DataGridComponentFramework {
	private static String SECURITY_LEVEL = DatagridPageConfiguration.SECURITY_LEVEL;
	private static String EMPLOYEE_DETAILS = DatagridPageConfiguration.EMPLOYEE_DETAILS;
	private static String BRANCH_DETAILS = DatagridPageConfiguration.BRANCH_DETAILS;
	private static String DIVISION_DETAILS = DatagridPageConfiguration.DIVISION_DETAILS;
	private static String EMPLOYEE_QUALIFICATION_DETAILS = DatagridPageConfiguration.EMPLOYEE_QUALIFICATION_DETAILS;
	private static String EMPLOYEE_LEAVE_DETAILS = DatagridPageConfiguration.EMPLOYEE_LEAVE_DETAILS;
	private static String EMPLOYEE_LEAVE_APPROVE = DatagridPageConfiguration.EMPLOYEE_LEAVE_APPROVE;
	private static String PUBLIC_HOLIDAYS = DatagridPageConfiguration.PUBLIC_HOLIDAYS;
	private static String LIEU_LEAVE_REQUEST = DatagridPageConfiguration.LIEU_LEAVE_REQUEST;
	private static String APPROVE_LIEU_LEAVE = DatagridPageConfiguration.APPROVE_LIEU_LEAVE;
	private static String APPROVE_OVER_TIME = DatagridPageConfiguration.APPROVE_OVER_TIME;
	private static String OVER_TIME_REQUEST = DatagridPageConfiguration.OVER_TIME_REQUEST;

	private AdministrationService administrationService = new AdministrationServiceImplementer();
	private EmployeeService employeeService = new EmployeeServiceImplementer();
	private EmployeeLeaveService employeeLeaveService = new EmployeeLeaveServiceImplementer();

	private Properties props  = FieldNamesProperties.getProps();
	private int fieldCount = 0;

	public List<String> getFieldNames(String aDataGridPageID) throws FrontierVisibleException {
		List<String> dataGridFieldNames = new ArrayList<String>();
		try {
			fieldCount = Integer.parseInt(props.getProperty(aDataGridPageID +"_COLUMN_COUNT"));
			for (int i=1; i<= fieldCount; i++) {
				dataGridFieldNames.add(props.getProperty(aDataGridPageID+"_FIELD_"+i));
			}
		} catch (Exception e) {
			throw new FrontierVisibleException(GeneralException.SYSTEM_ERROR_INVALID_PAGE_ID, null);
		}
		return dataGridFieldNames;
	}

	public int getDataFieldColumnCount(String aDataGridPageID) throws FrontierVisibleException {
		int dataFieldCount = 0;
		try {
			dataFieldCount = Integer.parseInt(props.getProperty(aDataGridPageID +"_DATA_COLUMN_COUNT"));
		} catch (Exception e) {
			throw new FrontierVisibleException(GeneralException.SYSTEM_ERROR_INVALID_PAGE_ID, null);
		}
		return dataFieldCount;
	}
	@SuppressWarnings("rawtypes")
	public List<List> getFieldDataForPage(String aDataGridPageID, int aStartingIndex, int aNumberOfRecords, long aCurrentLoginUserId) throws FrontierVisibleException {
		List<List> dataGridFieldData = null;
		if (aDataGridPageID.equalsIgnoreCase(SECURITY_LEVEL)) {
			dataGridFieldData = administrationService.getAvailableSecurityLevelsPage(aStartingIndex, aNumberOfRecords);
		} else if (aDataGridPageID.equalsIgnoreCase(BRANCH_DETAILS)) {
			dataGridFieldData = administrationService.getAvailableBranchesPage(aStartingIndex, aNumberOfRecords);			
		} else if (aDataGridPageID.equalsIgnoreCase(DIVISION_DETAILS)) {
			dataGridFieldData = administrationService.getAvailableDivisionsPage(aStartingIndex, aNumberOfRecords);
		} else if (aDataGridPageID.equalsIgnoreCase(EMPLOYEE_QUALIFICATION_DETAILS)) {
			dataGridFieldData = employeeService.getEmployeeQualificationPage(aStartingIndex, aNumberOfRecords);
		} else if (aDataGridPageID.equalsIgnoreCase(EMPLOYEE_LEAVE_APPROVE)) {
			dataGridFieldData = employeeLeaveService.getEmployeeLeaveApprovalRequestPage(aStartingIndex, aNumberOfRecords, aCurrentLoginUserId);
		} else if (aDataGridPageID.equalsIgnoreCase(APPROVE_LIEU_LEAVE)) {
			dataGridFieldData = employeeLeaveService.getEmployeeLieuLeaveApprovalRequestPage(aStartingIndex, aNumberOfRecords, aCurrentLoginUserId);
		} else if (aDataGridPageID.equalsIgnoreCase(APPROVE_OVER_TIME)) {
			dataGridFieldData = employeeService.getEmployeeOverTimeApprovalRequestPage(aStartingIndex, aNumberOfRecords, aCurrentLoginUserId);
		} else if (aDataGridPageID.equalsIgnoreCase(PUBLIC_HOLIDAYS)) {
			dataGridFieldData = administrationService.getAvailablePublicHolidayPage(aStartingIndex, aNumberOfRecords);
		} else  {
			throw new FrontierVisibleException(GeneralException.SYSTEM_ERROR_INVALID_PAGE_ID, null);
		}
		return dataGridFieldData;
	}

	@SuppressWarnings("rawtypes")
	public List<List> getFieldDataForPage(String aDataGridPageID, Map<String, Object> aQueryCriterias, int aStartingIndex, int aNumberOfRecords) throws FrontierVisibleException {
		List<List> dataGridFieldData = null;
		if (aDataGridPageID.equalsIgnoreCase(EMPLOYEE_LEAVE_DETAILS)) {
			dataGridFieldData = employeeLeaveService.getEmployeeLeaveDetailsPage(aQueryCriterias, aStartingIndex, aNumberOfRecords);	
		} else if (aDataGridPageID.equalsIgnoreCase(LIEU_LEAVE_REQUEST)) {
			dataGridFieldData = employeeLeaveService.getEmployeeLieuLeaveRequestPage(aQueryCriterias, aStartingIndex, aNumberOfRecords);
		} else if (aDataGridPageID.equalsIgnoreCase(OVER_TIME_REQUEST)) {
			dataGridFieldData = employeeService.getEmployeeOverTimeRequestPage(aQueryCriterias, aStartingIndex, aNumberOfRecords);	
		} else  {
			throw new FrontierVisibleException(GeneralException.SYSTEM_ERROR_INVALID_PAGE_ID, null);
		}
		return dataGridFieldData;
	}

	@SuppressWarnings("rawtypes")
	public List<List> getSearchResultFieldDataForPage(String aDataGridPageID, Object aSearchCriteria, int aStartingIndex, int aNumberOfRecords) throws FrontierVisibleException {
		List<List> dataGridFieldData = null;
		if (aDataGridPageID.equalsIgnoreCase(EMPLOYEE_DETAILS)) {
			dataGridFieldData = employeeService.getSearchResultPage((EmployeeSearch)aSearchCriteria, aStartingIndex, aNumberOfRecords);	
		} else {
			throw new FrontierVisibleException(GeneralException.SYSTEM_ERROR_INVALID_PAGE_ID, null);
		}
		return dataGridFieldData;
	}

	public int getRowCount(String aDataGridPageID, long aCurrentLoginUserId) throws FrontierVisibleException {
		int rowCount = 0;
		if (aDataGridPageID.equalsIgnoreCase(SECURITY_LEVEL)) {
			rowCount = administrationService.getAvailableSecurityLevelRecordCount();
		} else if (aDataGridPageID.equalsIgnoreCase(BRANCH_DETAILS)) {
			rowCount = administrationService.getAvailableBranchDetailRecordCount();
		} else if (aDataGridPageID.equalsIgnoreCase(DIVISION_DETAILS)) {
			rowCount = administrationService.getAvailableDivisionDetailRecordCount();
		} else if (aDataGridPageID.equalsIgnoreCase(EMPLOYEE_QUALIFICATION_DETAILS)) {
			rowCount = employeeService.getAvailableEmployeeQualificationDetailRecordCount();
		} else if (aDataGridPageID.equalsIgnoreCase(EMPLOYEE_LEAVE_APPROVE)) {
			rowCount = employeeLeaveService.getAvailableEmployeeLeaveApprovalRequestRecordCount(aCurrentLoginUserId);
		} else if (aDataGridPageID.equalsIgnoreCase(APPROVE_LIEU_LEAVE)) {
			rowCount = employeeLeaveService.getAvailableEmployeeLieuLeaveApprovalRequestRecordCount(aCurrentLoginUserId);
		} else if (aDataGridPageID.equalsIgnoreCase(APPROVE_OVER_TIME)) {
			rowCount = employeeService.getAvailableEmployeeOverTimeApprovalRequestRecordCount(aCurrentLoginUserId);
		} else if (aDataGridPageID.equalsIgnoreCase(PUBLIC_HOLIDAYS)) {
			rowCount = administrationService.getAvailablePublicHolidayRecordCount();
		} else  {
			throw new FrontierVisibleException(GeneralException.SYSTEM_ERROR_INVALID_PAGE_ID, null);
		}
		return rowCount;
	}

	public int getRowCount(Map<String, Object> aQueryCriterias, String aDataGridPageID) throws FrontierVisibleException {
		int rowCount = 0;
		if (aDataGridPageID.equalsIgnoreCase(EMPLOYEE_LEAVE_DETAILS)) {
			rowCount = employeeLeaveService.getAvailableEmployeeLeaveRequestsRecordCount(aQueryCriterias);
		} else if (aDataGridPageID.equalsIgnoreCase(LIEU_LEAVE_REQUEST)) {
			rowCount = employeeLeaveService.getAvailableEmployeeLieuLeaveRequestsRecordCount(aQueryCriterias);
		} else if (aDataGridPageID.equalsIgnoreCase(OVER_TIME_REQUEST)) {
			rowCount = employeeService.getAvailableEmployeeOverTimeRequestsRecordCount(aQueryCriterias);
		} else  {
			throw new FrontierVisibleException(GeneralException.SYSTEM_ERROR_INVALID_PAGE_ID, null);
		}
		return rowCount;
	}

	public int getRowCount(Object aSearchCriteria, String aDataGridPageID) throws FrontierVisibleException {
		int rowCount = 0;
		if (aDataGridPageID.equalsIgnoreCase(EMPLOYEE_DETAILS)) {
			rowCount = employeeService.getAvailableEmployeeDetailRecordCount((EmployeeSearch)aSearchCriteria);
		} else  {
			throw new FrontierVisibleException(GeneralException.SYSTEM_ERROR_INVALID_PAGE_ID, null);
		}
		return rowCount;
	}
}
